package com.example.zzm.yuejian03.Fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.example.zzm.yuejian03.Adapter.ClubAdapter;
import com.example.zzm.yuejian03.Bean.ClubBean;
import com.example.zzm.yuejian03.Listener.ClubListener;
import com.example.zzm.yuejian03.Model.ClubModel;
import com.example.zzm.yuejian03.Model.ClublwModel;
import com.example.zzm.yuejian03.Model.ClubzjModel;
import com.example.zzm.yuejian03.R;

import java.util.List;

public class ClubFragment extends BasesFragment{
    private List<ClubBean> list;
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager  layoutManager;
    private ClubAdapter adapter;
    private Button button5,button6,button7;
    private View view;

    private ClubListener listener = new ClubListener() {

        @Override
        public void onResponse(List<ClubBean> beanlist) {
            list=beanlist;
            adapter.setList(list);
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(context, "失败："+msg, Toast.LENGTH_SHORT).show();

        }
    };
    public ClubFragment() {    }
    @Nullable
    @Override //生命周期方法，创建View
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_club,container,false);
        initView();
        initEvent();
        return view;
    }

    void initView(){
        button5 = (Button) view.findViewById(R.id.button5);
        button6 = (Button) view.findViewById(R.id.button6);
        button7 = (Button) view.findViewById(R.id.button7);
    }

    void initEvent(){
        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClubModel model=new ClubModel();
                model.getClubList(listener);
            }
        });
        button6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClubzjModel model=new ClubzjModel();
                model.getClubList(listener);
            }
        });

        button7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClublwModel model=new ClublwModel();
                model.getClubList(listener);
            }
        });
    }

    @Override//生命周期方法，View创建完成
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
       // System.out.println("--f1--"+getSessionId());
        initRecyclerView(view);
        ClubModel model=new ClubModel();
        model.getClubList(listener);
    }
    private void initRecyclerView(View view) {
        recyclerView=view.findViewById(R.id.recyclerview);
        layoutManager=new LinearLayoutManager(context);
        GridLayoutManager gridLayoutManager =new GridLayoutManager(getContext(),2);

        recyclerView.setLayoutManager(gridLayoutManager);
        //每个item如果是确定高度，设置此项提高性能
        recyclerView.setHasFixedSize(true);
        //实例化适配器
        adapter=new ClubAdapter(context);
        recyclerView.setAdapter(adapter);
    }

    public interface OnFragmentInteractionListener {
    }

}